<template>
  <div class="content-list-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="标题">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入内容标题"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="searchForm.type" placeholder="请选择类型" clearable style="width: 120px">
            <el-option label="文章" value="article" />
            <el-option label="视频" value="video" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="searchForm.category" placeholder="请选择分类" clearable style="width: 120px">
            <el-option label="认知提升" value="cognition" />
            <el-option label="健身运动" value="fitness" />
            <el-option label="学习成长" value="learning" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 120px">
            <el-option label="已发布" value="1" />
            <el-option label="草稿" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作区域 -->
    <el-card class="operation-card">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>
        添加内容
      </el-button>
      <el-button type="danger" :disabled="!multipleSelection.length" @click="handleBatchDelete">
        <el-icon><Delete /></el-icon>
        批量删除
      </el-button>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="封面" width="120">
          <template #default="{ row }">
            <el-image
              v-if="row.coverImage"
              :src="row.coverImage"
              style="width: 60px; height: 40px"
              fit="cover"
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" min-width="250" show-overflow-tooltip />
        <el-table-column label="类型" width="100">
          <template #default="{ row }">
            <el-tag :type="row.type === 'article' ? 'primary' : 'success'">
              {{ row.type === 'article' ? '文章' : '视频' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="分类" width="120">
          <template #default="{ row }">
            {{ getCategoryName(row.category) }}
          </template>
        </el-table-column>
        <el-table-column prop="author" label="作者" width="120" />
        <el-table-column prop="viewCount" label="浏览量" width="100" />
        <el-table-column prop="likeCount" label="点赞数" width="100" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'published' ? 'success' : 'warning'">
              {{ row.status === 'published' ? '已发布' : '草稿' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              :type="scope.row.status === 1 ? 'warning' : 'success'"
              size="small"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 1 ? '下架' : '发布' }}
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const tableData = ref([])
const multipleSelection = ref([])

// 搜索表单
const searchForm = reactive({
  title: '',
  type: '',
  category: '',
  status: ''
})

// 分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 分类映射
const categoryMap = {
  cognition: '认知提升',
  fitness: '健身运动',
  learning: '学习成长'
}

// 生命周期
onMounted(() => {
  fetchContentList()
})

// 获取分类名称
const getCategoryName = (category) => {
  return categoryMap[category] || category
}

// 获取内容列表
const fetchContentList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    setTimeout(() => {
      const mockData = [
        {
          id: 1,
          title: '如何提高认知能力：从思维模式开始',
          type: 'article',
          category: 'cognition',
          author: '张三',
          coverImage: 'https://picsum.photos/300/200?random=1',
          viewCount: 1234,
          likeCount: 89,
          status: 1,
          createTime: '2023-01-01 10:00:00'
        },
        {
          id: 2,
          title: '健身入门指南：从零开始的运动计划',
          type: 'video',
          category: 'fitness',
          author: '李四',
          coverImage: 'https://picsum.photos/300/200?random=2',
          viewCount: 2345,
          likeCount: 156,
          status: 1,
          createTime: '2023-01-02 10:00:00'
        },
        {
          id: 3,
          title: '高效学习方法论：如何快速掌握新技能',
          type: 'article',
          category: 'learning',
          author: '王五',
          coverImage: 'https://picsum.photos/300/200?random=3',
          viewCount: 987,
          likeCount: 67,
          status: 0,
          createTime: '2023-01-03 10:00:00'
        }
      ]
      tableData.value = mockData
      pagination.total = mockData.length
      loading.value = false
    }, 500)
  } catch (error) {
    console.error('获取内容列表失败:', error)
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.currentPage = 1
  fetchContentList()
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 添加内容
const handleAdd = () => {
  router.push('/content/add')
}

// 编辑内容
const handleEdit = (row) => {
  router.push(`/content/edit/${row.id}`)
}

// 删除内容
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除内容 "${row.title}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟删除操作
    ElMessage.success('删除成功')
    fetchContentList()
  }).catch(() => {
    // 取消删除
  })
}

// 批量删除
const handleBatchDelete = () => {
  const titles = multipleSelection.value.map(item => item.title).join('、')
  ElMessageBox.confirm(`确定要删除选中的内容吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟批量删除操作
    ElMessage.success('批量删除成功')
    fetchContentList()
  }).catch(() => {
    // 取消删除
  })
}

// 切换内容状态
const handleToggleStatus = (row) => {
  const action = row.status === 1 ? '下架' : '发布'
  ElMessageBox.confirm(`确定要${action}内容 "${row.title}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟状态切换操作
    row.status = row.status === 1 ? 0 : 1
    ElMessage.success(`${action}成功`)
  }).catch(() => {
    // 取消操作
  })
}

// 表格选择变化
const handleSelectionChange = (selection) => {
  multipleSelection.value = selection
}

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.pageSize = size
  fetchContentList()
}

// 当前页变化
const handleCurrentChange = (page) => {
  pagination.currentPage = page
  fetchContentList()
}
</script>

<style scoped>
.content-list-container {
  padding: 20px;
}

.search-card,
.operation-card,
.table-card {
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 0;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>